package edu.fit5046.mymonashmate.activity;

import edu.fit5046.mymonashmate.R;
import edu.fit5046.mymonashmate.utils.DetailInfo;
import edu.fit5046.mymonashmate.utils.Utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class RegisterActivity extends Activity implements OnClickListener {

	public static final int INFO_END = DetailInfo.INFO_PREVIOUSJOB + 1;

	private EditText editTextRegInfo[];
	private EditText editTextID;
	private EditText editTextPassword1;
	private EditText editTextPassword2;

	private LinearLayout layoutUsr;
	private LinearLayout layoutInfo;

	private boolean isInfoView = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		setContentView(R.layout.layout_register);
		initWidget();

		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.button_register_next:
			next();
			break;

		case R.id.button_register_cancel:
			setResult(RESULT_OK);
			finish();
			break;

		default:
			break;
		}

	}

	private void initWidget() {

		editTextRegInfo = new EditText[INFO_END];
		editTextRegInfo[DetailInfo.INFO_NAME] = (EditText) findViewById(R.id.text_Name);
		editTextRegInfo[DetailInfo.INFO_SURNAME] = (EditText) findViewById(R.id.text_SurName);
		editTextRegInfo[DetailInfo.INFO_NICKMAKE] = (EditText) findViewById(R.id.text_NickName);
		editTextRegInfo[DetailInfo.INFO_COURSE] = (EditText) findViewById(R.id.text_Course);
		editTextRegInfo[DetailInfo.INFO_UNIT] = (EditText) findViewById(R.id.text_Unit);
		editTextRegInfo[DetailInfo.INFO_NATIONNANITY] = (EditText) findViewById(R.id.text_NNanity);
		editTextRegInfo[DetailInfo.INFO_FIRSTLANGUAGE] = (EditText) findViewById(R.id.text_FLanguage);
		editTextRegInfo[DetailInfo.INFO_SECONDLANGUAGE] = (EditText) findViewById(R.id.text_SLanguage);
		editTextRegInfo[DetailInfo.INFO_ADDRESS] = (EditText) findViewById(R.id.text_address);
		editTextRegInfo[DetailInfo.INFO_FAVOURITEFOOD] = (EditText) findViewById(R.id.text_FFood);
		editTextRegInfo[DetailInfo.INFO_FAVOURITEMOIVE] = (EditText) findViewById(R.id.text_FMovie);
		editTextRegInfo[DetailInfo.INFO_FAVOURITEUNIT] = (EditText) findViewById(R.id.text_FUnit);
		editTextRegInfo[DetailInfo.INFO_FAVOURITEPROLAN] = (EditText) findViewById(R.id.text_FProLanguage);
		editTextRegInfo[DetailInfo.INFO_CURRENTJOB] = (EditText) findViewById(R.id.text_CJob);
		editTextRegInfo[DetailInfo.INFO_PREVIOUSJOB] = (EditText) findViewById(R.id.text_PJob);

		editTextID = (EditText) findViewById(R.id.edit_StuID);
		editTextPassword1 = (EditText) findViewById(R.id.edit_Password);
		editTextPassword2 = (EditText) findViewById(R.id.edit_RePassword);

		layoutUsr = (LinearLayout) findViewById(R.id.layout_register_usr);
		layoutInfo = (LinearLayout) findViewById(R.id.layout_register_info);

		Button registerButton = (Button) findViewById(R.id.button_register_next);
		Button cancelButton = (Button) findViewById(R.id.button_register_cancel);
		registerButton.setOnClickListener(this);
		cancelButton.setOnClickListener(this);
	}

	private void next() {

		DetailInfo info = new DetailInfo();

		if (!isInfoView) {
			if (setUser(info) != 0) {
				return;
			}
			isInfoView = true;
			layoutUsr.setVisibility(View.GONE);
			layoutInfo.setVisibility(View.VISIBLE);
		} else {

			if (getInfoFromWidget(info) != 0) {
				return;
			}
			// upload to server
			
			Intent intent = new Intent();
			intent.setClass(this, MapActivity.class);
			startActivity(intent);

		}

		// DetailInfo info = new DetailInfo();
		//
		// Location location = Utils.getLocation(this);
		// double latitude = location.getLatitude();
		// double longitude = location.getLongitude();
		// Log.v("tag", "latitude " + latitude + "  longitude:" + longitude);

	}

	@SuppressLint("NewApi")
	private int setUser(DetailInfo info) {
		Log.i("TAG", "setUser");
		String tmp;
		tmp = editTextID.getText().toString();
		if (tmp.isEmpty()) {
			Toast.makeText(this, "ID shoun't be null", Toast.LENGTH_LONG)
					.show();
			setFocus(editTextID);
			return -1;
		}
		info.ID = Integer.valueOf(tmp);
		tmp = editTextPassword1.getText().toString();
		if (!editTextPassword2.getText().toString().equals(tmp)) {
			Toast.makeText(this, "password is not same", Toast.LENGTH_LONG)
					.show();
			setFocus(editTextPassword1);
			return -1;
		}
		info.password = tmp;
		return 0;
	}

	@SuppressLint("NewApi")
	private int getInfoFromWidget(DetailInfo info) {
		String value;
		for (int i = 0; i < INFO_END; i++) {
			value = editTextRegInfo[i].getText().toString();
			if (value.isEmpty()) {
				if (!checkCanBeNull(i)) {
					Toast.makeText(this, "* must be input", Toast.LENGTH_LONG)
							.show();
					setFocus(editTextRegInfo[i]);
					return -1;
				}
			}
			info.SetInfo(i, value);
		}
		return 0;

	}

	private void setFocus(EditText editText) {
		editText.setFocusable(true);
		editText.setFocusableInTouchMode(true);
		editText.requestFocus();
		editText.requestFocusFromTouch();

	}

	private boolean checkCanBeNull(int index) {
		if ((index == DetailInfo.INFO_NAME)
				|| (index == DetailInfo.INFO_COURSE)
				|| (index == DetailInfo.INFO_UNIT))
			return false;
		return true;
	}

}
